var express = require('express');
var router = express.Router();
var orm = require('orm');
//DB inform
var dbusername = "root";
var dbpassword = "root";
var dbname = "bd_news";

/* 获取主页 */
router.get('/index', function(req, res, next) {
	res.render('index', {});
});

/* 显示主页 */
router.get('/', function(req, res, next) {
	res.render('index', {});
});


/* 新闻资讯 */
router.use('/news', orm.express("mysql://" + dbusername + ":" + dbpassword + "@localhost/" + dbname, {
	define: function(db, models, next) {
		models.news = db.define("news", {
			id: {
				type: 'serial',
				key: true
			},
			type_new: String,
			title: String,
			content: String,
			ctime: String,
			utime: String
		});

		next();
	}
}));

/* 获取新闻资讯 */
router.get('/news', function(req, res, next) {
	var type = req.query.type;
	var type_new="";
	if(type==1){
		type_new="新闻";
	}else if(type==2){
		type_new="军事";
	}else if(type==3){
		type_new="娱乐";
	}else{
		type_new="笑话";
	}
	req.models.news.find({ type_new:type_new},function(err, news) {

		if (err) throw err;

		res.status(200);
		res.json(news);
	});


});


module.exports = router;
